package flc.ast.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.midi.lib.model.MidFileProvider;
import com.stark.piano.lib.widget.PianoSongPlayer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.listener.OnPageChangeListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import flc.ast.R;
import flc.ast.activity.PianoActivity;
import flc.ast.activity.PianoTrainActivity;
import flc.ast.activity.PracticeReportActivity;
import flc.ast.adapter.HotMusicAdapter;
import flc.ast.adapter.IndicatorAdapter;
import flc.ast.adapter.MyBannerAdapter;
import flc.ast.bean.PracticeTime;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.DBEngine;
import flc.ast.utils.PracticeDatabase;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FastClickUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private IndicatorAdapter indicatorAdapter;
    private HotMusicAdapter hotMusicAdapter;


    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        mDataBinding.tvPracticeReport.setOnClickListener(this);
        mDataBinding.ivStartPractice.setOnClickListener(this);
        List<Integer> list = new ArrayList<>();
        list.add(R.drawable.b1);
        list.add(R.drawable.b2);
        list.add(R.drawable.b3);
        MyBannerAdapter bannerAdapter = new MyBannerAdapter(list);
        mDataBinding.homeBanner.setAdapter(bannerAdapter, true);//false不无限循环
        mDataBinding.homeBanner.setCurrentItem(4);
        mDataBinding.homeBanner.isAutoLoop(true);//false不自动轮播
        mDataBinding.homeBanner.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                indicatorAdapter.setSelPosition(position);
                indicatorAdapter.notifyDataSetChanged();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mDataBinding.rvIndicatorList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        indicatorAdapter = new IndicatorAdapter();
        indicatorAdapter.setSelPosition(0);
        indicatorAdapter.setList(list);
        mDataBinding.rvIndicatorList.setAdapter(indicatorAdapter);
        mDataBinding.rvMusicList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        hotMusicAdapter = new HotMusicAdapter();
        mDataBinding.rvMusicList.setAdapter(hotMusicAdapter);
        hotMusicAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvPracticeReport:
                startActivity(PracticeReportActivity.class);
                break;
            case R.id.ivStartPractice:
                startActivity(PianoActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        PianoTrainActivity.pianoTrainMode= PianoSongPlayer.Mode.APPRECIATE;
        PianoTrainActivity.playFileBean=hotMusicAdapter.getItem(position);
        startActivity(PianoTrainActivity.class);
    }

    @Override
    protected void initData() {
        hotMusicAdapter.setList(MidFileProvider.getHot());
    }

    @Override
    public void onResume() {
        super.onResume();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
        //今日练琴时长
        long today = TimeUtils.string2Millis(TimeUtils.getNowString(new SimpleDateFormat("yyyy.MM.dd")), new SimpleDateFormat("yyyy.MM.dd"));
        Log.e("TAG", "onResume: "+today );
        List<PracticeTime> todayPractice = PracticeDatabase.getInstance(mContext).getPracticeDao().getTodayPracticeTime(today);
        if (todayPractice.size() == 0) {
            mDataBinding.tvPracticeToday.setText("00:00:00");
        } else {
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
            dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
            mDataBinding.tvPracticeToday.setText(TimeUtils.millis2String(todayPractice.get(0).getDuration(), dateFormat));
        }
        //周练琴时长
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.clear(Calendar.MINUTE);
        calendar.clear(Calendar.SECOND);
        calendar.clear(Calendar.MILLISECOND);
        long startTime = calendar.getTimeInMillis();
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        long endTime = calendar.getTimeInMillis();
        int count = 0;
        long duration = 0;
//        Log.e("TAG", "startTime: " + startTime);
//        Log.e("TAG", "endTime: " + endTime);

        List<PracticeTime> weekPractice = PracticeDatabase.getInstance(mContext).getPracticeDao().getWeekPracticeTime(startTime, endTime);

        if (weekPractice.size() == 0) {
            mDataBinding.tvPracticeNum.setText("0");
            mDataBinding.tvPracticeTime.setText("00:00:00");
        } else {
            for (PracticeTime practiceTime : weekPractice) {
                count += practiceTime.getCount();
                duration += practiceTime.getDuration();
            }
            mDataBinding.tvPracticeNum.setText(count + "");
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
            dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
            mDataBinding.tvPracticeTime.setText(TimeUtils.millis2String(duration, dateFormat));
        }
//            }
//        }).start();
    }
}